<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bug" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体部分 -->
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      width="55%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="showDialog" :id="curId" :is-edit="isEdit" @ :origin-list="originList" @success="hSuccess" @close="hClose" />
    </el-dialog>
    <!-- 当封装相同业务的组件时, 很容易碰到数据残留的bug， 主要时dialog隐藏没有销毁组件，导致数据残留 -->
    <!--
      解决方法:
      1. 用v-if人为的方式让组件销毁
      2. 随着dialog显示隐藏,手动调用子组件的方法,重新加载数据(不是特别推荐,有bug需要再次解决)
      $refs 获取原生 DOM 对象或子组件对象, 获取子组件对象后可以调用组件的方法,可以访问子组件的属性
      3. 在子组件中监听 id 的变化, 如果在编辑状态下则发起请求 (不是特别推荐)
     -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialogEdit"
      width="55%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="showDialogEdit" :id="curId" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { deleteDepartmentById, getDepartmentsList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      showDialogEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    async loadDepartmentList() {
      try {
        const res = await getDepartmentsList()
        // 删除传智
        res.data.depts.shift()
        // this.originList = res.data.depts.map(item => ({ id: item.id, pid: item.pid, name: item.name, code: item.code }))
        this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log(e)
      }
    },
    hShowAdd(id) {
      this.showDialog = true
      // 获取当前id 传给子组件
      this.curId = id
      // 将状态修改为修改
      this.isEdit = false
    },
    hSuccess() {
      this.showDialog = false
      this.showDialogEdit = false
      // 获取最新的部门信息
      this.loadDepartmentList()
    },
    hShowEdit(id) {
      // 显示编辑的 dialog
      this.showDialogEdit = true
      // console.log(data.id)
      // 获取当前被点击的部门 id, 传给子组件
      this.curId = id
      // 将状态修改为添加
      this.isEdit = true
    },
    hDel(id) {
      // 提示用户真的要删除吗
      this.$confirm('确定删除吗?', '提示', {
        type: 'warning'
      })
        .then(async() => {
          try {
            // 发请求
            const res = await deleteDepartmentById(id)
            // 根据结果提醒用户
            this.$message.success(res.message)
            // 重新渲染页面
            this.loadDepartmentList()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    },
    hClose() {
      this.showDialog = false
      this.showDialogEdit = false
    }
  }
}
</script>
